package com.example.photo.controller;

import com.example.photo.common.AuthCheck;
import com.example.photo.common.BaseResponse;
import com.example.photo.common.ErrorCode;
import com.example.photo.exception.BusinessException;
import com.example.photo.model.VO.PrizeVO;
import com.example.photo.model.VO.ScoreWorkVO;
import com.example.photo.model.domain.Prize;
import com.example.photo.model.request.add.NewPrizeRequest;
import com.example.photo.model.request.update.UpdatePrizeRequest;
import com.example.photo.service.PrizeService;
import com.example.photo.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 奖项管理接口
 *
 * @author Lwh
 * @time 2023/04/01 16:38
 */
@RestController
@RequestMapping("/prize")
public class PrizeController {

    @Resource
    private PrizeService prizeService;

    /**
     * 管理员创建奖项，其中所属赛事id为空，等待后续其他接口填充
     */
    @PostMapping("/new")
    @AuthCheck
    public BaseResponse<PrizeVO> createPrize(@RequestBody NewPrizeRequest newPrizeRequest) {
        if (newPrizeRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        PrizeVO prizeVO = prizeService.createPrize(newPrizeRequest);
        if (prizeVO == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "新建比赛失败");
        }
        return ResultUtils.success(prizeVO);
    }

    /**
     * 根据奖项id查询奖项信息
     */
    @GetMapping("/get")
    public BaseResponse<PrizeVO> getPrizeById(@RequestParam Integer prizeId) {
        if (prizeId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Prize prize = prizeService.getById(prizeId);
        if (prize == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "查询的奖项信息不存在");
        }
        return ResultUtils.success(new PrizeVO(prize));
    }

    /**
     * 管理员修改奖项信息
     */
    @AuthCheck
    @PostMapping("/update")
    public BaseResponse<PrizeVO> updatePrizeById(@RequestBody UpdatePrizeRequest updatePrizeRequest) {
        if (updatePrizeRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        PrizeVO prizeVO = prizeService.updatePrize(updatePrizeRequest);
        if (prizeVO == null) {
            throw new BusinessException(ErrorCode.SERVER_ERROR);
        }
        //重新获取奖项信息并返回
        return ResultUtils.success(prizeVO);
    }

    /**
     * 管理员删除奖项
     */
    @AuthCheck
    @PostMapping("/remove")
    public BaseResponse<Boolean> removePrizeById(@RequestParam Integer prizeId) {
        if (prizeId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //判断是否已有作品获得该奖项
        Prize prize = prizeService.getById(prizeId);
        if (prize == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "该奖项不存在");
        }
        if (!Objects.equals(prize.getTotalNum(), prize.getVacantNum())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不能删除已颁出的奖项");
        }
        boolean remove = prizeService.removeById(prizeId);
        if (!remove) {
            throw new BusinessException(ErrorCode.SERVER_ERROR, "删除奖项失败");
        }
        return ResultUtils.success(true);
    }

    /**
     * 颁奖页获取所有作品及其均分
     *
     * @param contestId 赛事id
     * @return 包含作品均分的作品信息列表
     */
    @AuthCheck
    @GetMapping("/works")
    public BaseResponse<List<ScoreWorkVO>> listToPrize(@RequestParam long pageSize,
                                                       @RequestParam long pageNum,
                                                       @RequestParam Integer contestId) {
        if (contestId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<ScoreWorkVO> list = prizeService.listToPrize(pageSize, pageNum, contestId);
        return ResultUtils.success(list);
    }

    /**
     * 根据作品id获取能颁的奖（赛事和组别相符）
     *
     * @param workId 作品id
     * @return 奖项信息列表
     */
    @AuthCheck
    @GetMapping("/elections")
    public BaseResponse<List<PrizeVO>> getPrizesByWorkId(@RequestParam Integer workId) {
        if (workId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<PrizeVO> prizeVOS = prizeService.getPrizes(workId);
        return ResultUtils.success(prizeVOS);
    }

    /**
     * 管理员给某个作品颁奖（重复颁奖会修改）
     *
     * @param workId  作品id
     * @param prizeId 奖项id
     */
    @AuthCheck
    @PostMapping("/confer")
    public BaseResponse<Boolean> conferPrize(@RequestParam Integer workId,
                                             @RequestParam Integer prizeId) {
        if (workId < 1 || prizeId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        prizeService.conferPrize(workId, prizeId);
        return ResultUtils.success(true);
    }

    /**
     * 撤销奖项
     *
     * @param workId 作品id
     */
    @AuthCheck
    @PostMapping("/withdrawal")
    public BaseResponse<Boolean> withdrawalPrize(@RequestParam Integer workId) {
        if (workId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        prizeService.withdrawalPrize(workId);
        return ResultUtils.success(true);
    }

}
